package com._520it.crm.service.impl;

import com._520it.crm.domain.Client;
import com._520it.crm.domain.ClientReport;
import com._520it.crm.domain.Employee;
import com._520it.crm.mapper.ClientMapper;
import com._520it.crm.mapper.ClientReportMapper;
import com._520it.crm.mapper.EmployeeMapper;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.QueryObject;
import com._520it.crm.service.IClientService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by 呆 on 2017/10/3.
 */
//业务里欠缺employee录入人

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;
    @Autowired
    private ClientReportMapper clientReportMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return clientMapper.deleteByPrimaryKey(id);
    }

    /**
     * 涉及到每个负责人的潜在客户增加和正式客户增加
     *
     * @param client
     * @return
     */
    @Override
    public int insert(Client client) {


        //当前登录人
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        client.setInputUser(employee.getId());
        client.setInputTime(new Date());
        clientMapper.insert(client);
        //--------------------------------客户报表报表-------------------------------
        //得出顾的负责人
        Long princiUser = client.getPrinciUser();
        Integer status = client.getStatus();
        if (status != Client.SOURCE_CLIENT ){
            //查出id对应员工的名字
            Employee princiUserEmployee = employeeMapper.selectById(princiUser);
            String princiName = princiUserEmployee.getName();
            //根据id和状态查出唯一一条记录
            if (status != null) {
                ClientReport clientReport = clientReportMapper.selectByprinciUserAndstatus(princiUser, status);
                if (clientReport != null) {
                    //客户的状态
                    if (status == Client.HIDDEN_CLIENT) {
                        //如果是为潜在客户,就得到该负责人潜在客户的数量
                        Integer hiddenNumber = clientReport.getHiddenNumber();

                            hiddenNumber++;

                        clientReport.setHiddenNumber(hiddenNumber);

                    } else if (status == Client.FORMAL_CLIENT) {
                        Integer formalNumber = clientReport.getFormalNumber();

                            formalNumber++;

                        clientReport.setFormalNumber(formalNumber);
                    }
                    clientReportMapper.updateByPrimaryKey(clientReport);

                } else {
                    ClientReport newClientReport = new ClientReport();
                    newClientReport.setPrinciUser(princiUser);
                    newClientReport.setName(princiName);
                    newClientReport.setStatus(client.getStatus());
                    if (status == Client.HIDDEN_CLIENT) {
                        newClientReport.setHiddenNumber(1);

                    } else if (status == Client.FORMAL_CLIENT) {
                        newClientReport.setFormalNumber(1);
                    }
                    clientReportMapper.insert(newClientReport);
                }
            }
        }

        return 0;
    }

    @Override
    public Client selectByPrimaryKey(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Client client) {

        return clientMapper.updateByPrimaryKey(client);
    }

    public int updateByPrimaryClient(Client client) {

        client.setEditTime(new Date());

        return clientMapper.updateByPrimaryClient(client);
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = clientMapper.queryforCount(qo);
        if (totalCount == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Client> data = clientMapper.queryforList(qo);
        return new PageResult(totalCount, data);
    }


    public void selectClient(Client client,Long orderBillId,String recognizee,String carOwner) {
        Client clientExist=clientMapper.selectClient(client.getIdCardType(),client.getIdCardNumber());
        if (clientExist==null){
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            client.setInputUser(employee.getId());
            client.setInputTime(new Date());
            client.setStatus(Client.FORMAL_CLIENT);
            client.setPrinciUser(employee.getId());
            clientMapper.insert(client);
            //----------------------------报表业务:lyx----------------------------------
            //得出顾的负责人

            Integer status = client.getStatus();
            //查出id对应员工的名字
            Employee princiUserEmployee = employeeMapper.selectById(client.getPrinciUser());
            String princiName = princiUserEmployee.getName();
            //根据id和状态查出唯一一条记录
            if (status != null) {
                ClientReport clientReport = clientReportMapper.selectByprinciUserAndstatus(client.getPrinciUser(), status);
                if (clientReport != null) {
                    Integer formalNumber = clientReport.getFormalNumber();
                    if (formalNumber > 0) {
                        formalNumber++;
                    }
                    clientReport.setFormalNumber(formalNumber);
                    clientReportMapper.updateByPrimaryKey(clientReport);
                } else {
                    ClientReport newClientReport = new ClientReport();
                    newClientReport.setPrinciUser(client.getPrinciUser());
                    newClientReport.setName(princiName);
                    newClientReport.setStatus(client.getStatus());
                    newClientReport.setFormalNumber(1);
                    clientReportMapper.insert(newClientReport);
                }

            }
            //orderBillMapper.updataApplyClientIdCardNumber(orderBillId,client.getIdCardNumber());
        } else {
            //orderBillMapper.updataApplyClientIdCardNumber(orderBillId,client.getIdCardNumber());
            int i = 1 / 0;
        }

    }

    @Override
    public void saveRecognizee(Client client, Long orderBillId) {
        Client clientExist = clientMapper.selectClient(client.getIdCardType(), client.getIdCardNumber());
        if (clientExist == null) {
            clientMapper.insert(client);
            //----------------------------报表业务:lyx----------------------------------

            //得出顾的负责人
            Long princiUser = client.getPrinciUser();
            Integer status = client.getStatus();
            //查出id对应员工的名字
            Employee princiUserEmployee = employeeMapper.selectById(princiUser);
            String princiName = princiUserEmployee.getName();
            //根据id和状态查出唯一一条记录
            if (status != null) {
                ClientReport clientReport = clientReportMapper.selectByprinciUserAndstatus(princiUser, status);
                if (clientReport != null) {
                    Integer hiddenNumber = clientReport.getHiddenNumber();

                        hiddenNumber++;

                    clientReport.setHiddenNumber(hiddenNumber);
                    clientReportMapper.updateByPrimaryKey(clientReport);
                } else {
                    ClientReport newClientReport = new ClientReport();
                    newClientReport.setPrinciUser(princiUser);
                    newClientReport.setName(princiName);
                    newClientReport.setStatus(client.getStatus());
                    newClientReport.setHiddenNumber(1);
                    clientReportMapper.insert(newClientReport);
                }

            }

            orderBillMapper.updataRecognizeeIdCardNumber(orderBillId, client.getIdCardNumber());
        } else {
            orderBillMapper.updataRecognizeeIdCardNumber(orderBillId, client.getIdCardNumber());
        }
    }

    @Override
    public Client selectApplyClient(Long id) {
        return clientMapper.selectApplyClient(id);
    }

    @Override
    public Client selectRecognizee(Long id) {
        return clientMapper.selectRecognizee(id);
    }

    @Override
    public Client selectCarOwner(Long id) {
        return clientMapper.selectCarOwner(id);
    }


    /**
     * 潜在客户转为正式客户的业务方法
     * 执行报表业务,分为转正前,转正后,两种状态
     * 单一状态无法执行业务报表
     * 原因:1.转正前 潜在客户必须-1;潜在状态+负责人id 形成一条已有的sql 或新的sql
     * 2.状态转正后,转正状态+负责人人id形成一条已有的sql 或新的sql
     */

    @Override
    public void changeFormalClientById(Long id) {
        //转正前客户状态
        Client client = clientMapper.selectByPrimaryKey(id);
        //-------------------------------------报表业务---------------------------------
        //得出顾的负责人
        Long princiUser = client.getPrinciUser();
        //负责人的名字
        Employee employee = employeeMapper.selectById(princiUser);
        String name = employee.getName();
        //客户状态
        Integer status = client.getStatus();

        //根据id和状态查出唯一一条记录
        //转正之前的clientreport
        ClientReport clientReport = clientReportMapper.selectByprinciUserAndstatus(princiUser, status);

        Integer hiddenNumber = clientReport.getHiddenNumber();
        hiddenNumber--;
        clientReport.setHiddenNumber(hiddenNumber);
        //转正前 更新
        clientReportMapper.updateByPrimaryKey(clientReport);

        //---------------------------------转正操作----------------------------------
        clientMapper.changeFormalClientById(id, Client.FORMAL_CLIENT);
        client.setEditTime(new Date());
        client.setStatus(Client.FORMAL_CLIENT);
        clientMapper.updateByPrimaryKey(client);
        //-------------------------------------报表业务---------------------------------
        //转正之后的client
        Client formalClient = clientMapper.selectByPrimaryKey(id);
        //转正之后的clientReport
        //查看数据库是否有这个记录
        ClientReport formalClientReport = clientReportMapper.selectByprinciUserAndstatus(princiUser, formalClient.getStatus());

        if (formalClientReport != null) {
            Integer formalNumber = formalClientReport.getFormalNumber();
            formalNumber++;
            formalClientReport.setFormalNumber(formalNumber);
            clientReportMapper.updateByPrimaryKey(formalClientReport);
        } else {
            ClientReport newClientReport = new ClientReport();
            newClientReport.setFormalNumber(1);
            newClientReport.setPrinciUser(princiUser);
            newClientReport.setName(name);
            newClientReport.setStatus(formalClient.getStatus());
            clientReportMapper.insert(newClientReport);
        }

    }


    /**
     * 潜在客户转为资源池客户的业务方法
     */
    @Override
    public void changeSourceFormalClientById(Long id) {

        Client client = clientMapper.selectByPrimaryKey(id);

        //-------------------------------------报表业务---------------------------------
        //得出顾的负责人
        Long princiUser = client.getPrinciUser();
        Integer status = client.getStatus();
        //查出id对应员工的名字
        Employee princiUserEmployee = employeeMapper.selectById(princiUser);
        String princiName = princiUserEmployee.getName();

        //根据id和状态查出唯一一条报表记录
        ClientReport clientReport = clientReportMapper.selectByprinciUserAndstatus(princiUser, status);
        //潜在客户的数量
        Integer hiddenNumber = clientReport.getHiddenNumber();
        //-1;
        hiddenNumber--;
        //改本身的数据
        clientReport.setHiddenNumber(hiddenNumber);
        //清掉关联的负责人和负责人id


        //更新报表
        clientReportMapper.updateByPrimaryKey(clientReport);

        clientMapper.changeFormalClientById(id, Client.SOURCE_CLIENT);
        //清掉负责人相关的信息
        /*client.setPrinciUser(null);*/
        //放进池里的操作
        clientMapper.changeFormalClientById(id, Client.SOURCE_CLIENT);
        client.setEditTime(new Date());
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        client.setInputUser(employee.getId());
        client.setStatus(Client.SOURCE_CLIENT);
        clientMapper.updateByPrimaryKey(client);

    }


    @Override
    public PageResult employeeQuery(QueryObject qo) {
        Integer totalCount = clientMapper.employeeQueryforCount(qo);
        if (totalCount == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Client> data = clientMapper.employeeQueryforList(qo);
        return new PageResult(totalCount, data);
    }

    //吸纳功能业务方法
    @Override
    public void attactClient(Long clientId) {
        Client client = clientMapper.selectByPrimaryKey(clientId);
        //当前需要吸纳客户的员工
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //需要员工的id作为查询条件
        Long id = employee.getId();
        //查找报表里是否有这条记录
        ClientReport clientReport = clientReportMapper.selectByprinciUserAndstatus(id, Client.HIDDEN_CLIENT);
        if (clientReport != null){
            //找出他的增加潜在客户数量
            Integer hiddenNumber = clientReport.getHiddenNumber();
            //增加1;
            hiddenNumber++;
            //设置值
            clientReport.setHiddenNumber(hiddenNumber);
            //更新
            clientReportMapper.updateByPrimaryKey(clientReport);
        }else{
            ClientReport newClientReport = new ClientReport();
            newClientReport.setPrinciUser(id);
            newClientReport.setName(employee.getName());
            newClientReport.setStatus(Client.HIDDEN_CLIENT);
            newClientReport.setHiddenNumber(1);
            clientReportMapper.insert(newClientReport);

        }
        //吸纳以后更新客户的状态
        client.setStatus(Client.HIDDEN_CLIENT);
        client.setPrinciUser(id);
        client.setEditTime(new Date());
        clientMapper.updateByPrimaryKey(client);
    }


}
